package cn.ibizlab.todo.core.pms.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.todo.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.todo.core.pms.domain.ProjectTeam;
import cn.ibizlab.todo.core.pms.filter.ProjectTeamSearchContext;
import cn.ibizlab.todo.core.pms.service.IProjectTeamService;

import cn.ibizlab.todo.util.helper.CachedBeanCopier;
import cn.ibizlab.todo.util.helper.DEFieldCacheMap;


import cn.ibizlab.todo.core.pms.client.ProjectTeamFeignClient;

/**
 * 实体[项目团队] 服务对象接口实现
 */
@Slf4j
@Service
public class ProjectTeamServiceImpl implements IProjectTeamService {

    @Autowired
    ProjectTeamFeignClient projectTeamFeignClient;


    @Override
    public boolean create(ProjectTeam et) {
        ProjectTeam rt = projectTeamFeignClient.create(et);
        if(rt==null)
            return false;
        CachedBeanCopier.copy(rt,et);
        return true;
    }

    public void createBatch(List<ProjectTeam> list){
        projectTeamFeignClient.createBatch(list) ;
    }

    @Override
    public boolean update(ProjectTeam et) {
        ProjectTeam rt = projectTeamFeignClient.update(et.getId(),et);
        if(rt==null)
            return false;
        CachedBeanCopier.copy(rt,et);
        return true;

    }

    public void updateBatch(List<ProjectTeam> list){
        projectTeamFeignClient.updateBatch(list) ;
    }

    @Override
    public boolean remove(Long id) {
        boolean result=projectTeamFeignClient.remove(id) ;
        return result;
    }

    public void removeBatch(Collection<Long> idList){
        projectTeamFeignClient.removeBatch(idList);
    }

    @Override
    public ProjectTeam get(Long id) {
		ProjectTeam et=projectTeamFeignClient.get(id);
        if(et==null){
            et=new ProjectTeam();
            et.setId(id);
        }
        else{
        }
        return  et;
    }

    @Override
    public ProjectTeam getDraft(ProjectTeam et) {
        et=projectTeamFeignClient.getDraft();
        return et;
    }

    @Override
    public boolean checkKey(ProjectTeam et) {
        return projectTeamFeignClient.checkKey(et);
    }
    @Override
    @Transactional
    public boolean save(ProjectTeam et) {
        if(et.getId()==null) et.setId((Long)et.getDefaultKey(true));
        if(!projectTeamFeignClient.save(et))
            return false;
        return true;
    }

    @Override
    public void saveBatch(List<ProjectTeam> list) {
        projectTeamFeignClient.saveBatch(list) ;
    }



	@Override
    public List<ProjectTeam> selectByRoot(Long id) {
        ProjectTeamSearchContext context=new ProjectTeamSearchContext();
        context.setSize(Integer.MAX_VALUE);
        context.setN_root_eq(id);
        return projectTeamFeignClient.searchDefault(context).getContent();
    }

    @Override
    public void removeByRoot(Long id) {
        Set<Long> delIds=new HashSet<Long>();
        for(ProjectTeam before:selectByRoot(id)){
            delIds.add(before.getId());
        }
        if(delIds.size()>0)
            this.removeBatch(delIds);
    }

    @Autowired
    @Lazy
    IProjectTeamService proxyService;
	@Override
    public void saveByRoot(Long id,List<ProjectTeam> list) {
        if(list==null)
            return;
        Set<Long> delIds=new HashSet<Long>();
        List<ProjectTeam> _update=new ArrayList<ProjectTeam>();
        List<ProjectTeam> _create=new ArrayList<ProjectTeam>();
        for(ProjectTeam before:selectByRoot(id)){
            delIds.add(before.getId());
        }
        for(ProjectTeam sub:list) {
            sub.setRoot(id);
            if(ObjectUtils.isEmpty(sub.getId()))
                sub.setId((Long)sub.getDefaultKey(true));
            if(delIds.contains(sub.getId())) {
                delIds.remove(sub.getId());
                _update.add(sub);
            }
            else
                _create.add(sub);
        }
        if(_update.size()>0)
            proxyService.updateBatch(_update);
        if(_create.size()>0)
            proxyService.createBatch(_create);
        if(delIds.size()>0)
            proxyService.removeBatch(delIds);
	}



    /**
     * 查询集合 数据集
     */
    @Override
    public Page<ProjectTeam> searchDefault(ProjectTeamSearchContext context) {
        Page<ProjectTeam> projectTeams=projectTeamFeignClient.searchDefault(context);
        return projectTeams;
    }




}



